package com.firefoxking.mysite.controller.interceptor;

import com.firefoxking.mysite.constant.WebConst;
import com.firefoxking.mysite.entity.Comment;
import com.firefoxking.mysite.entity.Options;
import com.firefoxking.mysite.service.options.OptionsService;
import com.firefoxking.mysite.utils.AdminCommons;
import com.firefoxking.mysite.utils.Commons;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class BaseInterceptor implements HandlerInterceptor {

    private static final Logger LOGGE = LoggerFactory.getLogger(BaseInterceptor.class);
    private static final String USER_AGENT = "user-agent";

    @Autowired
    private OptionsService optionsService;

    @Autowired
    private Commons commons;

    @Autowired
    private AdminCommons adminCommons;

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        Options ov = optionsService.getOptionsByName("site_record");
        request.setAttribute("commons", commons);//一些工具类和公共方法
        request.setAttribute("option", ov);
        request.setAttribute("adminCommons", adminCommons);
        initSiteConfig(request);
    }

    private void initSiteConfig(HttpServletRequest request) {
        if (WebConst.initConfig.isEmpty()) {
            List<Options> optionsList = optionsService.getOptions();
            Map<String, String> querys = new HashMap<>();
            optionsList.forEach(
                    options -> {
                        querys.put(options.getName(), options.getValue());
                    }
            );
            WebConst.initConfig = querys;
        }
    }


}
